<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-tabs v-model="user_type" @tab-click="getList(1)">
          <el-tab-pane :label="item.name" :name="item.user_type.toString()" v-for="(item,index) in headeNum" :key="index"/>
        </el-tabs>
        <div class="container">
          <el-form inline size="small" :model="userFrom" ref="userFrom" :label-position="labelPosition"  label-width="100px">
            <el-row>
              <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                <el-col v-bind="grid">
                  <el-form-item label="用户搜索：">
                    <el-input v-model="userFrom.name" placeholder="请输入姓名或手机号" clearable  class="selWidth"/>
                  </el-form-item>
                </el-col>
              </el-col>
              <template v-if="collapse">
                <el-col :xs="24" :sm="24" :md="24" :lg="18" :xl="18">
                  <el-col v-bind="grid">
                    <el-form-item label="性别：">
                      <el-radio-group v-model="userFrom.sex" type="button"  class="selWidth">
                        <el-radio-button label="">
                          <span>全部</span>
                        </el-radio-button>
                        <el-radio-button label="0">
                          <span>未知</span>
                        </el-radio-button>
                        <el-radio-button label="1">
                          <span>男</span>
                        </el-radio-button>
                        <el-radio-button label="2">
                          <span>女</span>
                        </el-radio-button>
                      </el-radio-group>
                    </el-form-item>
                  </el-col>
                </el-col>
              </template>
              <el-col  :xs="24" :sm="24" :md="24" :lg="6" :xl="6" class="text-right userFrom">
                <el-form-item>
                  <el-button label="default" class="" icon="el-icon-search" size="mini" type="primary"  @click="userSearchs">搜索</el-button>
                  <el-button class="ResetSearch mr10" @click="reset('userFrom')" icon="el-icon-refresh" size="mini">重置</el-button>
                  <a class="ivu-ml-8" @click="collapse = !collapse">
                    <template v-if="!collapse">
                      展开 <i class="el-icon-arrow-down"></i>
                    </template>
                    <template v-else>
                      收起 <i class="el-icon-arrow-up"></i>
                    </template>
                  </a>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </div>
      <el-table
        ref="table"
        v-loading="listLoading"
        :data="tableData.data"
        style="width: 100%"
        size="mini"
        :header-cell-style=" {fontWeight:'bold',background:'#f8f8f9',color: '#515a6e',height:'40px'}" border
        >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="近次访问时间：">
                <span>{{ props.row.last_login_time | filterEmpty }}</span>
              </el-form-item>
              <el-form-item label="近次访问IP：">
                <span>{{ props.row.last_login_ip | filterEmpty }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          prop="id"
          label="ID"
          min-width="60"
        />
        <el-table-column
        label="头像" min-width="80">
          <template slot-scope="scope">
            <div class="demo-image__preview">
              <el-image
                style="width: 36px; height: 36px"
                :src="scope.row.avatar"
                :preview-src-list="[scope.row.avatar]"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="真实姓名"
          min-width="120"
        >
          <template slot-scope="scope">
            <span>{{scope.row.name | filterEmpty  }} | {{scope.row.sex | sexFilter}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="手机号"
          min-width="120"
        >
          <template slot-scope="scope">
            <span>{{scope.row.phone_number | filterEmpty}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="身份证号"
          min-width="120"
        >
          <template slot-scope="scope">
            <span>{{scope.row.id_card | filterEmpty}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="认证状态"
          min-width="120"
        >
          <template slot-scope="scope">
            <span :style="scope.row.autherized===1?'color:#91cc75':'color:red'">{{scope.row.autherized_text}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="关联企业"
          min-width="120"
        >
          <template slot-scope="scope">
            <template v-if="scope.row.userAssociation">
              <el-button v-if="scope.row.userAssociation.audit===0" icon="el-icon-edit" type="primary" @click="onAssociationDetail(scope.row.id)" size="mini" v-hasPermi="['admin:user:association']">审核</el-button>
              <span v-if="scope.row.userAssociation.audit!==0" @click="onAssociationDetail(scope.row.id)" style="cursor: pointer;" :style="scope.row.userAssociation.audit===1?'color:#91cc75':'color:red'">{{scope.row.userAssociation.audit_text}}</span>
            </template>
            <template v-else>-</template>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="130" fixed="right" align="center">
          <template slot-scope="scope">
<!--            <el-button icon="el-icon-edit" type="primary" @click="editUser(scope.row.uid)" size="mini" v-hasPermi="['admin:user:info']" style="margin-right:10px;">编辑</el-button>-->
            <el-dropdown trigger="click">
              <span class="el-dropdown-link">
                更多<i class="el-icon-arrow-down el-icon--right" />
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="onDetails(scope.row.id)" v-if="checkPermi(['admin:user:info'])">用户详情</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
        </el-table>
      <div class="block">
        <el-pagination
          :page-sizes="[15, 30, 45, 60]"
          :page-size="userFrom.limit"
          :current-page="userFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <!--关联企业信息审核-->
    <el-dialog
      title="关联企业信息"
      :visible.sync="VisibleAssociation"
      width="700px"
      :close-on-click-modal="false"
      :before-close="handleAssociationClose">
      <el-form :model="associationValidateForm" :rules="rules" ref="associationValidateForm" label-width="90px" class="demo-dynamic" v-loading="loadingAssociation">
        <el-form-item label="企业名称">
          <div class="item-line">{{associationValidateForm.name | filterEmpty}}</div>
        </el-form-item>
        <el-form-item label="职位">
          <div class="item-line">{{associationValidateForm.position | filterEmpty}}</div>
        </el-form-item>
        <el-form-item label="营业执照">
          <div class="acea-row" v-if="associationValidateForm.business_license">
            <div
              class="pictrue mb10"
              draggable="true"
            >
              <el-image
                class="photo-el-image"
                :src="associationValidateForm.business_license"
                :preview-src-list="[associationValidateForm.business_license]"
              />
            </div>
          </div>
          <div v-else>-</div>
        </el-form-item>
        <el-form-item label="审核状态" prop="audit">
          <el-select v-model="associationValidateForm.audit" placeholder="状态" clearable class="selWidth">
            <el-option label="通过" :value="1"/>
            <el-option label="拒绝" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="associationValidateForm.remark" type="textarea" :rows="5" placeholder="备注" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleAssociationClose">取 消</el-button>
        <el-button type="primary" :loading="loadingBtn" @click="submitAssociationForm('associationValidateForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!--详情-->
    <el-dialog
      title="用户详情"
      :visible.sync="Visible"
      width="1100px"
    >
      <user-details ref="userDetails" :id="id" v-if="Visible" @close="close"></user-details>
    </el-dialog>
  </div>
</template>

<script>
  import { userListApi, userAssociationApi ,userAssociationCheckApi,groupListApi, levelListApi, tagListApi, groupPiApi, tagPiApi, foundsApi, updateSpreadApi, updatePhoneApi } from '@/api/user'
  import editFrom from './edit'
  import userDetails from './userDetails'
  import userList from '@/components/userList'
  import Cookies from 'js-cookie'
  import { checkPermi } from "@/utils/permission"; // 权限判断函数
  import {Debounce} from '@/utils/validate'
  import * as backupsApi from '@/api/backups'
  export default {
    name: 'UserIndex',
    components:{ editFrom, userDetails,userList },
    filters: {
      sexFilter(status) {
        const statusMap = {
          0: '未知',
          1: '男',
          2: '女',
        }
        return statusMap[status]
      },
      auditFilter(status) {
        const statusMap = {
          0: '待审核',
          1: '通过',
          2: '拒绝',
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        loadingBtn: false,
        Visible: false,
        labelPosition:'right',
        collapse: false,
        props: {
          children: 'child',
          label: 'name',
          value: 'name',
          emitPath: false
        },
        headeNum: [
          { 'user_type': '0', 'name': '全部用户' },
          //{ 'type': 'routine', 'name': '微信小程序用户' },
          { 'user_type': '1', 'name': '求职工' },
        ],
        listLoading: true,
        tableData: {
          data: [],
          total: 0
        },
        user_type: '0',
        userFrom: {
          sex: '',
          //accessType: 0,
          name: '',
          page: 1,
          limit: 15,
        },
        grid: {
          xl: 8,
          lg: 12,
          md: 12,
          sm: 24,
          xs: 24
        },

        //关联企业审核
        VisibleAssociation: false,
        associationValidateForm: {
          name: '',
          position: '',
          business_license: '',
          audit: null,
          remark:'',
          user_id:0,
        },
        rules: {
          audit: [{ required: true, message: '请选择审核状态', trigger: ['blur', 'change'] }],
          remark: [{ required: true, message: '请填备注', trigger: ['blur', 'change'] }],
        },
        loadingAssociation: false,

        loading: false,
        id: null,
      }
    },
    activated(){
      this.userFrom.name = '';
      this.user_type = '0';
      this.getList(1);
    },
    mounted() {
      this.getList()
    },
    methods: {
      checkPermi,

      //状态更新
      onAssociationDetail(id) {
        this.loadingAssociation = true
        userAssociationApi({id:id}).then(data => {
          this.associationValidateForm = data
          this.associationValidateForm.audit = this.associationValidateForm.audit === 0 ? null : this.associationValidateForm.audit
          this.loadingAssociation = false
        })

        this.VisibleAssociation = true
      },
      handleAssociationClose() {
        this.VisibleAssociation = false
        this.associationValidateForm = {
          name: '',
          position: '',
          business_license: '',
          audit: null,
          remark:'',
          user_id:0,
        }
      },
      submitAssociationForm:Debounce(function(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loadingBtn = true

            const _pram = {
              user_id: this.associationValidateForm.user_id,
              audit: this.associationValidateForm.audit,
              remark : this.associationValidateForm.remark
            }
            userAssociationCheckApi(_pram).then(res => {
              this.$message.success('审核成功')
              this.loadingBtn = false
              this.handleAssociationClose()
              this.getList()
            }).catch(() => {
              this.loadingBtn = false
            })
          } else {
            return false
          }
        })
      }),

      reset(formName) {
        this.userFrom = {
            sex: '',
            //accessType: 0,
            name: '',
            page: 1,
            limit: 15,
        }
        this.getList()
      },

      close() {
        this.Visible = false
        this.getList()
      },
      // 账户详情
      onDetails(id){
        this.id = id
        this.Visible = true
      },

      // 搜索
      userSearchs () {
        this.userFrom.page = 1;
        this.getList();
      },

      // 列表
      getList(num) {
        this.listLoading = true
        this.userFrom.page = num ? num : this.userFrom.page;
        this.userFrom.user_type = this.user_type
        //if(this.loginType == 0) this.userFrom.userType =''
        userListApi(this.userFrom).then(res => {
        //后期接口调用
          this.tableData.data = res.list
          this.tableData.total = res.count
          this.listLoading = false
        }).catch(() => {
          this.listLoading = false
        })
        //this.checkedCities = this.$cache.local.has('user_stroge') ? this.$cache.local.getJSON('user_stroge') : this.checkedCities;
      },

      pageChange(page) {
        this.userFrom.page = page
        this.getList()
      },
      handleSizeChange(val) {
        this.userFrom.limit = val
        this.getList()
      },
    }
  }
</script>

<style scoped lang="scss">
  /*.timeBox{*/
    /*width: 100%;*/
    /*::v-deep.el-form-item__content{*/
      /*width: 87% !important;*/
    /*}*/
  /*}*/
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
    font-size: 12px;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  .text-right{
    text-align: right;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 33.33%;
  }
  .selWidth{
    width: 100% !important;
  }
  .seachTiele{
    line-height: 30px;
  }
  .container{
    min-width: 821px;
    ::v-deep.el-form-item{
      width: 100%;
    }
    ::v-deep.el-form-item__content{
      width: 72%;
    }
  }
  .ivu-ml-8{
    font-size: 12px;
    color: #1682e6;
  }
  .btn_bt{
    border-top:1px dashed #ccc;
    padding-top: 20px;
  }
  .relative{
    position: relative;
  }
  .pictrue{
    width: 250px;
    height: 180px;
    border: 1px dotted rgba(0,0,0,0.1);
    margin-right: 10px;
    position: relative;
    cursor: pointer;
    .photo-el-image{
      width: 100%;
      height: 100%;
    }
    img{
      width: 100%;
      height: 100%;
    }
    video{
      width: 100%;
      height: 100%;
    }
  }
</style>
